using System;
using System.ServiceModel;
using System.ServiceModel.Channels;

namespace Albatross.WCF.Extenions.ServiceModel.Channels
{
    internal class CompressionOutputChannel<TChannelShape> : CompressionChannel<TChannelShape>,
                                                             IOutputChannel
        where TChannelShape : class, IOutputChannel
    {
        internal CompressionOutputChannel(ChannelManagerBase channelManagerBase,
                                          TChannelShape innerChannel)
            : base(channelManagerBase, innerChannel)
        {
        }

        #region IOutputChannel Members

        public IAsyncResult BeginSend(Message message,
                                      TimeSpan timeout,
                                      AsyncCallback callback,
                                      Object state)
        {
            return InnerChannel.BeginSend(message == null ? message : Compressor.CompressMessage(message)
                                          , timeout, callback, state);
        }

        public IAsyncResult BeginSend(Message message, AsyncCallback callback, object state)
        {
            return
                InnerChannel.BeginSend(message == null ? message : Compressor.CompressMessage(message), callback,
                                       state);
        }

        public void EndSend(IAsyncResult result)
        {
            InnerChannel.EndSend(result);
        }

        public EndpointAddress RemoteAddress
        {
            get { return InnerChannel.RemoteAddress; }
        }

        public void Send(Message message, TimeSpan timeout)
        {
            InnerChannel.Send(message == null ? message : Compressor.CompressMessage(message), timeout);
        }

        public void Send(Message message)
        {
            InnerChannel.Send(message == null ? message : Compressor.CompressMessage(message));
        }

        public Uri Via
        {
            get { return InnerChannel.Via; }
        }

        #endregion
    }
}